package com.sander.guardmanagement.ui.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sander.guardmanagement.R;
import com.sander.guardmanagement.http.BaseCallback;
import com.sander.guardmanagement.http.OkHttpHelper;
import com.sander.guardmanagement.http.UserSubmitCooperation;
import com.sander.guardmanagement.response.ResponseModel;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

public class ApplyCooperationActivity extends AppCompatActivity {


    public EditText suozaidiTextView;
    public EditText xiaoqutextview;
    public EditText compeenentextview;
    public EditText lianxirentextview;
    public EditText phonetextview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply_cooperation);


        initCallBack();

        Button sureButton = (Button)findViewById(R.id.sureqpplycooperation);
        sureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sureButtonAction();
            }
        });


        suozaidiTextView = (EditText) findViewById(R.id.nameinputview);
        xiaoqutextview = (EditText) findViewById(R.id.xiaoqutextview);
        compeenentextview = (EditText)findViewById(R.id.compeenentextview);
        lianxirentextview = (EditText) findViewById(R.id.lianxirentextview);
        phonetextview = (EditText) findViewById(R.id.phoneinputview);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

    }

    private void sureButtonAction(){
        // sure 事情
        String suozaidi = suozaidiTextView.getText().toString();
        String xiaoqu = xiaoqutextview.getText().toString();
        String compent = compeenentextview.getText().toString();
        String lianxiren = lianxirentextview.getText().toString();
        String phone = phonetextview.getText().toString();
        if (TextUtils.isEmpty(suozaidi)) showToastMessage("所在地信息不能为空");
        if (TextUtils.isEmpty(xiaoqu)) showToastMessage("小区信息不能为空");
        if (TextUtils.isEmpty(compent)) showToastMessage("公司信息不能为空");
        if (TextUtils.isEmpty(lianxiren)) showToastMessage("联系人信息不能为空");
        if (TextUtils.isEmpty(phone)) showToastMessage("手机不能为空");

        UserSubmitCooperation userSubmitCooperation = new UserSubmitCooperation();
        userSubmitCooperation.location = suozaidi;
        userSubmitCooperation.community = xiaoqu;
        userSubmitCooperation.company = compent;
        userSubmitCooperation.linkman = lianxiren;
        userSubmitCooperation.phone = phone;
        OkHttpHelper.getinstance().post(userSubmitCooperation.requestUrl(), userSubmitCooperation.getValueOfClas(), new BaseCallback<ResponseModel>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onSuccess(Response response, ResponseModel o) {
                if (o.getCode() == 0){
                    showToastMessage("提交成功");
                    finish();

                }else{
                    showToastMessage(o.getMsg());
                }

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

            }
        });


    }

    private void initCallBack(){
        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.callbackLayout);
        frameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    public void showToastMessage(String message){
        Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();

    }
}
